﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
//using CommunergyUploader.CommunergyUploadService;
using System.Diagnostics;
using Communergy.Repository.WCF.CommunergyUploadService;

namespace Communergy.Repository
{
    //TODO - move this to/call from WCFRepository
    public class CommunergyUpload : IHistoricRepository, IRealtimeRepository, IMeterRepository
    {
        int maxRecords = 50;
        
       

        #region IHistoricRepository Members

        public void Insert_ElectricityHourly(string meterId, List<HistoricalEnergyReading> hourlyEnergyReadings)
        {
            var client = new HistoricRepositoryClient();
            int recordsToUpload = (int)hourlyEnergyReadings.Count;
            int chunks = (recordsToUpload / maxRecords + 1);
            int recordsUploaded = 0;
            for (int chunk = 0; chunk < chunks; chunk++)
            {
                int startPosition = chunk * maxRecords;
                var chunkedData = hourlyEnergyReadings.Skip(startPosition).Take(maxRecords);
                client.Insert_ElectricityHourly(meterId,  chunkedData.ToList());
                recordsUploaded += chunkedData.Count();
            }
            Trace.WriteLine(string.Format("{0} of {1} records uploaded", recordsUploaded, hourlyEnergyReadings.Count));

            client.Close();
        }

        public void Insert_ElectricityDaily(string meterId, List<HistoricalEnergyReading> dailyEnergyReadings)
        {
            var client = new HistoricRepositoryClient();
            client.Insert_ElectricityDaily(meterId,  dailyEnergyReadings);
            client.Close();
        }

        public void Insert_ElectricityMonthly(string meterId, List<HistoricalEnergyReading> monthlyEnergyReadings)
        {
            var client = new HistoricRepositoryClient();
            client.Insert_ElectricityMonthly(meterId,  monthlyEnergyReadings);
            client.Close();
        }

        #endregion

        #region IRealtimeRepository Members

        public void Insert_ElectricityRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            RealtimeRepositoryClient client = new RealtimeRepositoryClient();
            int recordsToUpload = (int)realtimeEnergyReadings.Count;
            int chunks = (recordsToUpload / maxRecords + 1);
            int recordsUploaded = 0;
            for (int chunk = 0; chunk < chunks; chunk++)
            {
                int startPosition = chunk * maxRecords;
                var chunkedData = realtimeEnergyReadings.Skip(startPosition).Take(maxRecords);
                client.Insert_ElectricityRealtime(meterId, chunkedData.ToList());
                recordsUploaded += chunkedData.Count();
            }
            Trace.WriteLine(string.Format("{0} of {1} records uploaded", recordsUploaded, recordsToUpload));
            client.Close();
        }



        public List<RealtimeEnergyReading> Get_ElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            throw new NotImplementedException();
        }

       
        public RealtimeEnergyReading GetLastElectricityRealtime(string meterId)
        {
            RealtimeRepositoryClient client = new RealtimeRepositoryClient();
            var realtime = client.GetLastElectricityRealtime(meterId);
            client.Close();
            return realtime;
        }

        #endregion

        #region IMeterRepository Members

        public Meter CreateNewMeter(Meter meterToCreate)
        {
            var client = new MeterRepositoryClient();
            meterToCreate.MeterGuid = Guid.NewGuid().ToString();
            var newMeter = client.CreateNewMeter(meterToCreate);
            client.Close();
            return newMeter;
        }

        public Meter RetrieveMeterDetails(string meterId)
        {
            throw new NotImplementedException();
        }

        public void SaveMeterDetails(Meter meterToSave)
        {
            throw new NotImplementedException();
        }

        public List<Meter> GetMetersForUser(string userId)
        {
            var client = new MeterRepositoryClient();

            var meters = client.GetMetersForUser(userId);
            client.Close();
            return meters;
        }

        #endregion

        #region IHistoricRepository Members


        public List<HistoricalEnergyReading> GetHourly(string meterId, DateTime dateFrom)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IRealtimeRepository Members


        public HistoricalEnergyReading GetCumulativeElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
